import 'package:flutter/material.dart';
import 'package:flutter_lifecycle/flutter_lifecycle.dart';

class Nav2Page2 extends StatefulWidget {
  final VoidCallback? onHidePage2;

  const Nav2Page2({Key? key, required this.onHidePage2}) : super(key: key);

  @override
  State createState() {
    return _Nav2Page2State();
  }
}

class _Nav2Page2State extends State<Nav2Page2> {
  @override
  void initState() {
    super.initState();
    // log.add('Nav2Page2($hashCode)#initState()');
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // print('Nav2Page2($hashCode)#didChangeDependencies() route(${ModalRoute.of(context).hashCode})');
  }

  @override
  void dispose() {
    // log.add('Nav2Page2($hashCode)#dispose()');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return  BasePageLifecycle(
      onPageShow: (){
        debugPrint('Nav2Page2');
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Nav2'),
          leading: BackButton(
            onPressed: () {
              Navigator.of(context, rootNavigator: true).maybePop();
            },
          ),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                onPressed: () {
                  showDialog(
                    context: context,
                    // routeSettings: const RouteSettings(name: 'dialog'),
                    builder: (context) {
                      return AlertDialog(
                        content: const Text(
                          'This is a dialog.',
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: const Text('Dismiss'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                          TextButton(
                            child: const Text('Open nav2'),
                            onPressed: () {
                              Navigator.of(context).pushNamed('nav2');
                            },
                          ),
                        ],
                      );
                    },
                  );
                },
                child: const Text('show Alert Dialog'),
              ),
              TextButton(
                onPressed: widget.onHidePage2,
                child: const Text('Hide Page2'),
              ),
              TextButton(
                onPressed: (){
                  Navigator.of(context).pop();
                  // Navigator.of(context).pop();
                },
                child: const Text('pop '), //pop pop
              ),
            ],
          ),
        ),
      ),
    );
  }
}